package com.bookstore.bauhinia.controller;

import com.bookstore.bauhinia.service.AuthService;
import com.bookstore.bauhinia.pojo.CommonResult;
import com.bookstore.bauhinia.pojo.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class AuthController {

    @Resource
    private AuthService authService;

    @PostMapping("/auth/register")
    public CommonResult register(@RequestBody User user){
        List<User> list = authService.getUserByEmail(user.getEmail());
        if(list.size() > 0){
            return new CommonResult(443, "注册失败，邮箱已被注册");
        } else{
            if(authService.insertUser(user)){
                return new CommonResult(200, "注册成功");
            }
            return new CommonResult(500, "注册失败，网络异常");
        }
    }

    @PostMapping("/auth/login")
    public CommonResult login(@RequestBody User user){
        List<User> list = authService.getUserByEmail(user.getEmail());
        if(list.size() <= 0){
            return new CommonResult(443, "邮箱错误");
        }
        User costomer = list.get(0);
        if(costomer.getPassword().equals(user.getPassword())){
            return new CommonResult(200, "登录成功", costomer);
        }
        return new CommonResult(442, "邮箱或密码错误");
    }
}
